package com.example.myapplication.ui.components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog


data class IconOption(
    val type: String,
    val icon: ImageVector
)

/**
 * 添加储物空间对话框
 */
@Composable
fun AddStorageSpaceDialog(
    onDismiss: () -> Unit,
    onConfirm: (name: String, description: String?, iconType: String?) -> Unit,
    isEditMode: Boolean = false,
    initialName: String = "",
    initialDescription: String = "",
    initialIconType: String? = null
) {
    var name by remember { mutableStateOf(initialName) }
    var description by remember { mutableStateOf(initialDescription) }
    var selectedIconType by remember { mutableStateOf(initialIconType) }

    val iconOptions = remember {
        listOf(
            IconOption("box", Icons.Default.Inventory2),
            IconOption("folder", Icons.Default.Folder),
            IconOption("closet", Icons.Default.Checkroom),
            IconOption("bedroom", Icons.Default.BedroomParent),
            IconOption("kitchen", Icons.Default.Kitchen),
            IconOption("garage", Icons.Default.Garage),
            IconOption("office", Icons.Default.Work),
            IconOption("living_room", Icons.Default.Living),
            IconOption("electronics", Icons.Default.Devices),
            IconOption("books", Icons.Default.MenuBook),
            IconOption("sports", Icons.Default.SportsBaseball),
            IconOption("tools", Icons.Default.Build)
        )
    }

    Dialog(onDismissRequest = onDismiss) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            Column(
                modifier = Modifier.padding(24.dp),
                verticalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                Text(
                    text = if (isEditMode) "编辑储物空间" else "添加储物空间",
                    style = MaterialTheme.typography.headlineSmall
                )

                OutlinedTextField(
                    value = name,
                    onValueChange = { name = it },
                    label = { Text("空间名称") },
                    modifier = Modifier.fillMaxWidth(),
                    keyboardOptions = KeyboardOptions(
                        capitalization = KeyboardCapitalization.Words
                    ),
                    singleLine = true
                )

                OutlinedTextField(
                    value = description,
                    onValueChange = { description = it },
                    label = { Text("描述（可选）") },
                    modifier = Modifier.fillMaxWidth(),
                    maxLines = 3
                )

                Text(
                    text = "选择图标",
                    style = MaterialTheme.typography.titleMedium
                )
                LazyVerticalGrid(
                    columns = GridCells.Adaptive(minSize = 50.dp),
                    modifier = Modifier
                        .fillMaxWidth()
                        .heightIn(max = 180.dp),
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    items(iconOptions) { iconOption ->
                        val isSelected = selectedIconType == iconOption.type
                        Surface(
                            modifier = Modifier
                                .aspectRatio(1f)
                                .clickable {
                                    selectedIconType = if (isSelected) null else iconOption.type
                                },
                            shape = MaterialTheme.shapes.medium,
                            color = if (isSelected) MaterialTheme.colorScheme.primaryContainer else MaterialTheme.colorScheme.surface,
                            border = if (isSelected) BorderStroke(2.dp, MaterialTheme.colorScheme.primary) else BorderStroke(1.dp, MaterialTheme.colorScheme.outline)
                        ) {
                            Box(contentAlignment = Alignment.Center) {
                                Icon(
                                    imageVector = iconOption.icon,
                                    contentDescription = null, // Decorative
                                    modifier = Modifier.size(24.dp),
                                    tint = if (isSelected) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.onSurface
                                )
                            }
                        }
                    }
                }

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.End
                ) {
                    TextButton(onClick = onDismiss) {
                        Text("取消")
                    }
                    Spacer(modifier = Modifier.width(8.dp))
                    Button(
                        onClick = {
                            if (name.isNotBlank()) {
                                onConfirm(
                                    name.trim(),
                                    description.trim().takeIf { it.isNotEmpty() },
                                    selectedIconType
                                )
                            }
                        },
                        enabled = name.isNotBlank()
                    ) {
                        Text(if (isEditMode) "保存" else "添加")
                    }
                }
            }
        }
    }
}